package com.wdzg.member.${modular}.${modelName}.service.impl;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.wdzg.member.${modular}.${modelName}.service.${className}Service;
import com.wdzg.member.${modular}.${modelName}.mapper.${className}Mapper;
import com.wdzg.member.util.enumdata.DataEnum;
import com.wdzg.member.${modular}.utils.SnowflakeIdWorker;
import com.wdzg.member.${modular}.vo.${className};
import com.wdzg.member.${modular}.vo.${className}Example;

/**
 * 
 * @author generate
 */
@Service
public class ${className}ServiceImpl implements ${className}Service{

	@Autowired
	private	${className}Mapper ${className?uncap_first}Mapper;

	@Override
	public List<${className}> get${className}List(ConcurrentHashMap<String, Object> map, ${className} ${className?uncap_first}) {
		int page = Integer.parseInt(map.get("page").toString());
        int limit = Integer.parseInt(map.get("limit").toString());
        PageHelper.startPage(page, limit);
        
		${className}Example example = new ${className}Example();
		${className}Example.Criteria criteria= example.createCriteria();
		
		criteria.andStatusNotEqualTo(DataEnum.DELETE_STATUS.getStatus());
		if (StringUtils.isNotEmpty(${className?uncap_first}.getText())){
			criteria.andTextEqualTo(${className?uncap_first}.getText());
		}
		example.setOrderByClause(" id desc");
		
		return ${className?uncap_first}Mapper.selectByExample(example);
	}

	@Override
	public Integer getCountByExample(${className} ${className?uncap_first}) {
		${className}Example example = new ${className}Example();
		${className}Example.Criteria criteria= example.createCriteria();
		
		criteria.andStatusNotEqualTo(DataEnum.DELETE_STATUS.getStatus());
		if (StringUtils.isNotEmpty(${className?uncap_first}.getText())){
			criteria.andTextEqualTo(${className?uncap_first}.getText());
		}
		
		return ${className}Mapper.countByExample(example);
	}

	@Override
	public void saveOrUpdate(${className} ${className?uncap_first}) {
		if (${className?uncap_first}.getId() == null){
			${className?uncap_first}.setStatus(DataEnum.INSERT_STATUS.getStatus());
			${className?uncap_first}.setId(new SnowflakeIdWorker(0, 0).nextId());
			${className?uncap_first}Mapper.insertSelective(${className?uncap_first});
		} else {
			update(${className?uncap_first});
		}
	}

	private void update(${className} ${className?uncap_first}) {
		${className?uncap_first}Mapper.updateByPrimaryKeySelective(${className?uncap_first});
	}

	@Override
	public void delete(Long id) {
		${className} ${className?uncap_first} = getById(id);
		${className?uncap_first}.setStatus(DataEnum.DELETE_STATUS.getStatus());
		update(${className?uncap_first});
	}

	public ${className} getById(Long id) {
		return ${className?uncap_first}Mapper.selectByPrimaryKey(id);
	}

	@Override
	public List<${className}> get${className}All() {
		${className}Example example = new ${className}Example();
		${className}Example.Criteria criteria = example.createCriteria();
		
		criteria.andStatusEqualTo(DataEnum.INSERT_STATUS.getStatus());
		return ${className?uncap_first}Mapper.selectByExample(example);
	}
}
